package com.huabo.system.service.impl;


import com.hbfk.entity.DealUserToken;
import com.hbfk.entity.TblStaffUtil;
import com.hbfk.util.PageInfo;
import com.huabo.system.config.DateBaseConfig;
import com.huabo.system.mysql.entity.TblContractTypeofMySql;
import com.huabo.system.mysql.mapper.TblContractTypeofMySqlMapper;
import com.huabo.system.oracle.entity.TblContractTypeof;
import com.huabo.system.oracle.mapper.TblContractTypeofMapper;
import com.huabo.system.service.TblContractTypeofService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class TblContractTypeofServiceImpl implements TblContractTypeofService {
    @Resource
    private TblContractTypeofMapper tblContractTypeofMapper;

    @Resource
    private TblContractTypeofMySqlMapper tblContractTypeofMySqlMapper;


    @Override
    public Map<String, Object> findPageInfoList(Integer pageNumber, Integer pageSize, String choiceTypeName, String token, String staffId) {
        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        try {

            TblStaffUtil staff = DealUserToken.parseUserToken(token);


            if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
                PageInfo<TblContractTypeof> pageInfo = new PageInfo<TblContractTypeof>();
                TblContractTypeof condition = new TblContractTypeof();
                condition.setOrgId(staff.getCurrentOrg().getOrgid().intValue());
                //condition.setOrgId(2);
                if (choiceTypeName != null && !"".equals(choiceTypeName)) {
                    condition.setTypeName(choiceTypeName);
                }
                pageInfo.setCurrentPage(pageNumber);
                pageInfo.setPageSize(pageSize);
                pageInfo.setCondition(condition);
                pageInfo.setTlist(this.tblContractTypeofMapper.selectPageInfoList(pageInfo));
                pageInfo.setTotalRecord(this.tblContractTypeofMapper.selectPageInfoCount(pageInfo));
                resultMap.put("data", pageInfo);

            } else {

                PageInfo<TblContractTypeofMySql> pageInfo = new PageInfo<TblContractTypeofMySql>();
                TblContractTypeofMySql condition = new TblContractTypeofMySql();
                condition.setOrgId(staff.getCurrentOrg().getOrgid().intValue());
                //condition.setOrgId(2);
                if (choiceTypeName != null && !"".equals(choiceTypeName)) {
                    condition.setTypeName(choiceTypeName);
                }
                pageInfo.setCurrentPage(pageNumber);
                pageInfo.setPageSize(pageSize);
                pageInfo.setCondition(condition);
                pageInfo.setTlist(this.tblContractTypeofMySqlMapper.selectPageInfoList(pageInfo));
                pageInfo.setTotalRecord(this.tblContractTypeofMySqlMapper.selectPageInfoCount(pageInfo));
                resultMap.put("data", pageInfo);


            }


            resultMap.put("code", "1");
            resultMap.put("msg", "数据访问成功");


        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return resultMap;
    }

    @Override
    public TblContractTypeof findByid(String typeId) {

        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {

            List<TblContractTypeof> list = this.tblContractTypeofMapper.selectAllList(typeId);
            if (list != null && list.size() > 0) {
                return list.get(0);
            }
        } else {

            List<TblContractTypeofMySql> list = this.tblContractTypeofMySqlMapper.selectAllList(typeId);
            TblContractTypeof tblContractTypeof = new TblContractTypeof();

            if (list != null && list.size() > 0) {
                tblContractTypeof.setTypeId(list.get(0).getTypeId());
                tblContractTypeof.setTypeName(list.get(0).getTypeName());
                tblContractTypeof.setCreateStaff(list.get(0).getCreateStaff());
                tblContractTypeof.setCreateTime(list.get(0).getCreateTime());
                tblContractTypeof.setOrgId(list.get(0).getOrgId());
                tblContractTypeof.setUpdateStaff(list.get(0).getUpdateStaff());
                tblContractTypeof.setUpdateTime(list.get(0).getUpdateTime());
                tblContractTypeof.setPageUrl(list.get(0).getPageUrl());
                tblContractTypeof.setSettingid(list.get(0).getSettingid());
                return tblContractTypeof;
            }
        }


        return null;
    }

    @Override
    public void updateContractTypeof(TblContractTypeof typeof) {
        this.tblContractTypeofMapper.updateContractTypeof(typeof);
    }

    @Override
    public void updateMySqlContractTypeof(TblContractTypeofMySql typeofMySql) {
        this.tblContractTypeofMySqlMapper.updateContractTypeof(typeofMySql);
    }
}
